const router = require('koa-router')();
const { name, response, } = require('../public/javascripts/utils');
const item = require("../service/contacts")

router.prefix(`/${name(__filename)}`);
// 定义路由
router.get('/', async (ctx, next) => {
  try {
    const res = await item.findAll(ctx.query);
    response(ctx, res.res, res.total)
  } catch (err) {
    response(ctx,null,null,500, '服务器错误')
  }
});

// 定义路由
router.post('/save', async (ctx, next) => {
  try {
    const res = await item.create(ctx.request.body);
    response(ctx, res)
  } catch (err) {
    response(ctx,null,null,500, '服务器错误')
  }
});

// 定义路由
router.post('/delete', async (ctx, next) => {
  try {
    const res = await item.delete(ctx.request.body._id);
    response(ctx, res)
  } catch (err) {
    response(ctx,null,null,500, '服务器错误')
  }
});





module.exports = router;